<template>
  <div>
    <el-dialog title="批量转发" :visible.sync="dialogVisible" width="40%">
      <div>
        <el-row>
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">区域：</div>
          </el-col>
          <el-col :span="20">
            <el-row>
              <el-select v-model="value" placeholder="请选择">
                <el-option v-for="item in modelData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-row>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">收件人：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="to" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">抄送：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="cc" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">前缀：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="subjectPrefix" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">附件：</div>
          </el-col>
          <el-col :span="20">
            <el-select v-model="q.attachment" clearable placeholder="请选择" style="width:100%;">
            <el-option
              v-for="item in attachmentoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.disabled">
            </el-option>
          </el-select>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;display:none;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">主题：</div>
          </el-col>
          <el-col :span="20">
            <el-input placeholder="请输入内容" v-model="q.subject" clearable>
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top:1rem;">
          <el-col :span="3">
            <div style="text-align:right;padding:0.5rem;">正文：</div>
          </el-col>
          <el-col :span="20">
            <el-input type="textarea" placeholder="请输入内容" v-model="q.content" rows="5">
            </el-input>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doBatchForward()" v-loading.fullscreen.lock="fullscreenLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import _ from 'lodash'
  export default {
    data() {
      return {
        dialogVisible: false,
        data: [],
        fullscreenLoading: false,
        q: {
          to: [],
          cc: [],
          subject: "",
          content: "",
          id: "",
        },
        value: 1, //下拉框的值
        to: "", //文本框to的值
        cc: "", //文本框cc的值
        subjectPrefix:"",
        index:'',//遍历时的标记
        model:{},
        options: [],
        attachmentoptions:[],
        modelData: [],
        count: 0
      }
    },
    methods: {
      init(data) {
        this.dialogVisible = true
        this.data = data
        this.count = data.length
        this.getAttachment()
        this.getModels()
        
      },
      handleReceiver(val) {
        return val.split(",")
      },
      doBatchForward() {
        this.fullscreenLoading = true
        for (var i = 0; i < this.data.length; i++) {
          this.index = i
          this.q.resumeId = this.data[i].resumeId
          this.q.subject = this.computedSubject
          this.doforward()
        }

      },
      getAttachment(){
        this.$http({
          url: `${this.$http.adornUrl('/attachment/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          if(data.data.code==0){
            this.$message({
                message: '获取附件列表成功',
                type: 'success'
              });

              console.log(data)
              this.attachmentoptions = data.data.list
          }else{
            this.$message({
                message: '获取附件列表失败',
                type: 'error'
              });
          }
        })
      },
      doforward() {
        this.q.to = this.handleReceiver(this.to)
        this.q.cc = this.handleReceiver(this.cc)
        this.$http({
          url: `${this.$http.adornUrl('/resume/forward')}`,
          method: 'post',
          data: this.$http.adornData(this.q, false)
        }).then(({
          data
        }) => {
          this.count = this.count - 1
          if (data['code'] == 0) {
            this.$message({
              message: '发送邮件成功',
              type: 'success'
            });
          } else {
            this.$message({
              message: '发送邮件错误',
              type: 'error'
            });
          }
          if (this.count == 0) {
            this.dialogVisible = false
            this.fullscreenLoading = false
          }
        })
      },
      getModels() {
        this.$http({
          url: `${this.$http.adornUrl('/emailDict/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          console.log(data.data.data)
          this.modelData = data.data.data
          this.value = this.modelData[0]['id']
          this.coverModel(this.value)
        })
      },
      coverModel(val) {
        this.model = _.find(this.modelData, {
          'id': val
        })
        this.to = this.model.sendTo
        this.cc = this.model.cc
        this.q.content = this.model.content
        this.subjectPrefix = this.model.subjectPrefix
        this.q.attachment = this.model.attachment
      }

    },
    watch: {
      'value': function (newVal, oldVal) {
        this.coverModel(newVal)
      },
    },
    computed:{
      'computedSubject':function(){
          return this.subjectPrefix + this.data[this.index].appliedPosition + "_" + this.data[this.index].positionArea + "_" + this.data[this.index]
            .name + "_" +
            this.data[this.index].phoneNumber + "_" + this.data[this.index].age + "岁"
      }
    }
  }

</script>
